package org.malred.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.malred.hospital.common.R;

import org.malred.hospital.dto.UpdateDoctorDto;
import org.malred.hospital.entity.TbDoctor;
import org.malred.hospital.entity.TbStudent;
import org.malred.hospital.service.DoctorService;
import org.malred.hospital.utils.ConstValues;
import org.malred.hospital.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

//@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/doctor")
public class DoctorController extends BaseController<TbDoctor> {
    @Autowired
    private DoctorService service;

    @Override
    public R<List<TbDoctor>> findAll() {
        try {
            return R.success(service.list());
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/find/name/{name}")
    public R<List<TbDoctor>> findByName(@PathVariable String name) {
        LambdaQueryWrapper<TbDoctor> qw = new LambdaQueryWrapper<>();
        qw.like(TbDoctor::getName, name);
        List<TbDoctor> list = service.list(qw);
        if (list != null) {
            return R.success(list);
        }
        return R.error("[Doctor] 未找到");
    }

    @Override
    public R<TbDoctor> findById(@PathVariable Long id) {
        TbDoctor one = service.getById(id);
        if (one != null) {
            return R.success(one);
        }
        return R.error("[Doctor] 未找到");
    }

    @GetMapping("/find/{start}/{end}")
    public R<List<TbDoctor>> findInDate(@PathVariable String start,
                                        @PathVariable String end) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        LambdaQueryWrapper<TbDoctor> qw = new LambdaQueryWrapper<>();
        qw.ge(TbDoctor::getCreateTime, DateUtils.StringToDate(format, start));
        qw.le(TbDoctor::getCreateTime, DateUtils.StringToDate(format, end));
        List<TbDoctor> list = service.list(qw);

        return R.success(list);
    }

    @GetMapping("/page")
    public R<List<TbDoctor>> page(int page, int size) {
        //分页构造器
        Page<TbDoctor> pageInfo = new Page<>(page, size);
        //条件构造器
        LambdaQueryWrapper<TbDoctor> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(TbDoctor::getCreateTime);

        //分页查询
        service.page(pageInfo, queryWrapper);

        return R.success(pageInfo.getRecords());
    }

    @GetMapping("/count")
    public R<Integer> count() {
        return R.success(service.count());
    }

    @Override
    public R<TbDoctor> insert(@RequestBody TbDoctor dto) {
//        if (service.save(dto)) {
//            return R.success(dto);
//        } else {
//            return R.error("添加 [Doctor] 失败");
//        }
        return null;
    }

    @PostMapping("/dto/save")
    public R<TbDoctor> insertDto(@RequestBody UpdateDoctorDto dto) throws ParseException {
        if (dto == null) {
            return R.error("插入失败，医生信息为空");
        }
        if (dto.getName().isEmpty()) {
            return R.error("医生名称不能为空");
        }
//        if (!dto.getName().matches(ConstValues.CN_CHAT_REGEX) &&
//                !dto.getName().matches(ConstValues.EN_CHAT_REGEX)) {
//            return R.error("医生名称必须是中文或英文");
//        }
        if (!dto.getName().matches(ConstValues.EN_CHAT_REGEX) &&
                !dto.getName().matches(ConstValues.CN_CHAT_REGEX)) {
            return R.error("医生名称必须是中文或英文");
        }
        if (dto.getName().matches(ConstValues.OTHER_CHAT_REGEX)) {
            return R.error("医生名称不能含有特殊字符");
        }

        TbDoctor doctor = new TbDoctor();
        doctor.setName(dto.getName());
        doctor.setCreateTime(
                DateUtils.DateToLocalDateTime(
                        DateUtils.StringToDate(
                                new SimpleDateFormat("yyyy-MM-dd"),
                                dto.getCreateTime()
                        )
                )
        );
        doctor.setNumber(0L);

        if (service.save(doctor)) {
            return R.success(doctor);
        } else {
            return R.error("添加 [Doctor] 失败");
        }
    }

    @Override
    public R<TbDoctor> update(TbDoctor tbDoctor) {
        return null;
    }

    @PutMapping("/dto/save")
    public R<TbDoctor> updateDto(@RequestBody UpdateDoctorDto dto) throws ParseException {
        if (dto == null || dto.getName().isEmpty()) {
            return R.error("更新信息不能为空");
        }
        TbDoctor doctor = new TbDoctor();

        doctor.setId(dto.getId());
        doctor.setName(dto.getName());
        doctor.setNumber(dto.getNumber());

        Date date = DateUtils.StringToDate
                (new SimpleDateFormat("yyyy-MM-dd"), dto.getCreateTime());
        LocalDateTime localDateTime = DateUtils.DateToLocalDateTime(date);
        doctor.setCreateTime(localDateTime);

        if (service.saveOrUpdate(doctor)) {
            return R.success(doctor);
        } else {
            return R.error("修改 [Doctor] 失败");
        }
    }

    @Override
    public R<TbDoctor> delete(@PathVariable Long id) {
        if (service.removeById(id)) {
            return R.success(null);
        } else {
            return R.error("删除 [Doctor] 失败");
        }
    }
}
